﻿using Modules.Constructor;
using PmSoft;
using System.Security.Authentication;

namespace ManagementApi.Mvc.ApplicationContexts;

/// <summary>
/// 建设单位请求上下文
/// </summary>
[UseDI(ServiceLifetime.Scoped)]
public class ConstructorApplicationContext : ProjectUserApplicationContext
{
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="serviceProvider"></param>
    public ConstructorApplicationContext(IServiceProvider serviceProvider) : base(serviceProvider)
    {
    }

    private ConstructorInfo? _constructorInfo = null;

    /// <summary>
    /// 当前登录的建设单位信息
    /// </summary>
    public ConstructorInfo CurrentConstructor
    {
        get
        {
            if (_constructorInfo != null)
                return (ConstructorInfo)_constructorInfo;

            if (CurrentUser == null || CurrentUser is not ConstructorInfo constructorInfo)
                throw new AuthenticationException("当前非建设单位用户类型");
            _constructorInfo = constructorInfo;
            return _constructorInfo;
        }
    }
}
